﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace WhiteDemoWithWpf
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadProducts();
        }

        private void LoadProducts()
        {            
            using (var dc = new NorthwindDataContext())
            {
                var products = from p in dc.Products
                               select p;                
                if (String.IsNullOrEmpty(txtProductName.Text))
                {
                    DataContext = products.ToList();
                }
                else
                {
                    switch (lstFilterCriteria.SelectedIndex)
                    {
                        case 0:
                            DataContext = products.Where(p => p.ProductName.Contains(txtProductName.Text)).ToList();
                            break;
                        case 1:
                            DataContext = products.Where(p => p.ProductName.StartsWith(txtProductName.Text)).ToList();
                            break;
                        case 2:
                            DataContext = products.Where(p => p.ProductName.EndsWith(txtProductName.Text)).ToList();
                            break;
                        case 3:
                            DataContext = products.Where(p => p.ProductName.Equals(txtProductName.Text)).ToList();
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoadProducts();
        }
    }
}
